package com.onlinemarket.thrid.service;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import com.onlinemarket.thrid.daoimpl.SupplierDaoImpl;
import com.onlinemarket.thrid.daoimpl.registerImpl;
import com.onlinemarket.thrid.domain.Supplier;

/**
 * 供应商操作
 * 
 * @author LHC
 *
 */
@Controller
@RequestMapping(value = "/supplier")
public class SupplierControl {

	@Autowired
	private SupplierDaoImpl supplierdaoimpl;

	/**
	 * 跳转去添加供应商界面
	 * 
	 * @return
	 */
	@RequestMapping(value = "/toaddsupplier")
	public String toaddsupplier() {
		return "supplier/addsupplier";
	}

	@RequestMapping(value = "/addsupplier")
	public ModelAndView addsupplier(String supplier_name, String supplier_address, String supplier_telephone,
			String supplier_contact) {
		ModelAndView modelAndView = new ModelAndView();
		Supplier supplier = new Supplier();
		supplier.setSupplier_name(supplier_name);
		supplier.setSupplier_address(supplier_address);
		supplier.setSupplier_telephone(supplier_telephone);
		supplier.setSupplier_contact(supplier_contact);
		int insertnumber = supplierdaoimpl.insertSupplier(supplier);
		if (insertnumber == 1) {
			List<Supplier> supplier2 = supplierdaoimpl.getallSupplierforlist();
			modelAndView.addObject("supplier", supplier2);
			modelAndView.setViewName("supplier/supplierinfo");
		}
		return modelAndView;
	}

	/**
	 * 得到所有供应商信息
	 * 
	 * @return
	 */
	@RequestMapping(value = "/getallsupplier")
	public ModelAndView getallsupplier() {
		ModelAndView modelAndView = new ModelAndView();
		List<Supplier> supplier = supplierdaoimpl.getallSupplierforlist();
		modelAndView.addObject("supplier", supplier);
		modelAndView.setViewName("supplier/supplierinfo");
		return modelAndView;
	}

	/**
	 * 修改供应商信息
	 * 
	 * @return
	 */
	@RequestMapping(value = "/updateSupplierInfo")
	public ModelAndView updateSupplierInfo(int supplier_id) {
		ModelAndView modelAndView = new ModelAndView();
		Supplier supplier = supplierdaoimpl.selectSupplierById(supplier_id);
		modelAndView.addObject("supplier", supplier);
		modelAndView.setViewName("supplier/editsupplier");
		return modelAndView;
	}

	/**
	 * 删除供应商信息
	 * 
	 * @return
	 */
	@RequestMapping(value = "/deleteSupplierInfo")
	public ModelAndView deleteSupplite(int supplier_id) {
		ModelAndView modelAndView = new ModelAndView();
		boolean isdalate = supplierdaoimpl.deleteSuppier(supplier_id);
		if (isdalate) {
			List<Supplier> supplier = supplierdaoimpl.getallSupplierforlist();
			modelAndView.addObject("supplier", supplier);
			modelAndView.setViewName("supplier/supplierinfo");
		}
		return modelAndView;
	}

	/**
	 * 显示已修改的供应商信息
	 * 
	 * @param supplier_id
	 * @return
	 */
	@RequestMapping(value = "/showsupplier")
	public ModelAndView showsupplier(int supplier_id, String supplier_name, String supplier_address,
			String supplier_telephone, String supplier_contact) {
		ModelAndView modelAndView = new ModelAndView();
		Supplier supplier = new Supplier(supplier_id, supplier_name, supplier_address, supplier_telephone,
				supplier_contact);
		boolean isupdate = supplierdaoimpl.updateSupplierInfoById(supplier);
		if (isupdate) {
			Supplier supplierinfo = supplierdaoimpl.selectSupplierById(supplier_id);
			modelAndView.addObject("supplierinfo", supplierinfo);
			modelAndView.setViewName("supplier/showsupplier");
		}
		return modelAndView;
	}
}
